/**
 * @(#) ToApprovalProxy.as
 *
 * @date		2010. 09. 08.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @client		Network O&S 
 * @description	결재함 조회  Server Connection Proxy (결재함)
 * 				approval.ToApprovalProxy에서 복사..
 */
package com.skt.common_hr.approval_nos.model
{
	import com.skt.common_hr.approval_nos.model.vo.ApprovalListVO;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class ToApprovalProxy extends CommonProxy
	{
		public static const NAME:String = "ToApprovalProxy";
		
		public static const LOAD_TO_APPROVAL_LIST_COMPLETE:String 	= "loadToAppListComplete";
		public static const LOAD_TO_APPROVAL_LIST_FAILED:String 	= "loadToAppListFailed";
		public static const EXEC_ACCEPT_COMPLETE:String 			= "execApprovalComplete";
		public static const EXEC_ACCEPT_FAILED:String 			= "execApprovalFailed";
		public static const EXEC_REJECT_COMPLETE:String 				= "execDenyComplete";
		public static const EXEC_REJECT_FAILED:String 				= "execDenyFailed";
		public static const EXEC_DELEGATION_COMPLETE:String 		= "execDelegationComplete";
		public static const EXEC_DELEGATION_FAILED:String 			= "execDelegationFailed";
		public var appList:ArrayCollection;

		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function ToApprovalProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function loadList(pernr:String, status:String, begin:Date=null, end:Date=null):void
		{
			var params:Object = new Object;
			params["PERNR"] = pernr;
			params["WGBN"]  = "1";
			params["I_WSTATUS"] = status;
			//trace("loadList " + status);
			if(begin!=null) 
				params["I_BEGDA"] = DateUtil.toBasicString(begin);
			if(end!=null)
				params["I_ENDDA"] = DateUtil.toBasicString(end);
			loadData("Z_HR_0102", params, resultList, faultList);
		}
		
		public function acceptApproval(comment:String, appList:Array):void
		{
			var tables:Object = new Object;
			tables["ITAB"] = appList;
			
			var params:Object = new Object;
			params["I_WPERNR"] = Variables.myPernr;
			params["I_WSTATUS"] = "3";
			params["I_CMTXT"] = comment;
			//trace("execApproval: " + appList.length);
			saveData("Z_HR_0105", params, tables, resultExecApp, faultExecApp); 
		}
		
		
		public function rejectApproval(comment:String, appList:Array):void
		{
			var tables:Object = new Object;
			tables["ITAB"] = appList;
			
			var params:Object = new Object;
			params["I_WPERNR"] = Variables.myPernr;
			params["I_WSTATUS"] = "4";
			
			params["I_CMTXT"] = comment;
			saveData("Z_HR_0105", params, tables, resultDenyApp, faultDenyApp); 
		}
		
		public function delegateApproval(pernr:String, appList:Array):void
		{
			var tables:Object = new Object;
			tables["INPUT1"] = appList;
			
			var params:Object = new Object;
			params["PERNR"] = Variables.myPernr;
			params["SPERNR"] = pernr;
			//trace("위임을 호출했음 : " + pernr);
			saveData("Z_HR_0103", params, tables, resultDelegationApp, faultDelegationApp); 
		}
		
		private function resultList(event:ResultEvent):void
		{
			if(retCode==0) {
				appList = parseList(event.result.OUTPUT1);
				facade.sendNotification(LOAD_TO_APPROVAL_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_TO_APPROVAL_LIST_FAILED, this);
			}
		}
		
		private function faultList(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_TO_APPROVAL_LIST_FAILED, this);
		}
		
		private function resultExecApp(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(EXEC_ACCEPT_COMPLETE, this);
			}
			else {
				facade.sendNotification(EXEC_ACCEPT_FAILED, this);
			}
			
		}
		
		private function faultExecApp(event:FaultEvent):void
		{
			facade.sendNotification(EXEC_ACCEPT_FAILED, this);
		}
		
		private function resultDenyApp(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(EXEC_REJECT_COMPLETE, this);
			}
			else {
				facade.sendNotification(EXEC_REJECT_FAILED, this);
			}
			
		}
		private function faultDenyApp(event:FaultEvent):void
		{
			facade.sendNotification(EXEC_REJECT_FAILED, this);
		}

		private function resultDelegationApp(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(EXEC_DELEGATION_COMPLETE, this);
			}
			else {
				facade.sendNotification(EXEC_DELEGATION_FAILED, this);
			}
			
		}
		private function faultDelegationApp(event:FaultEvent):void
		{
			facade.sendNotification(EXEC_DELEGATION_FAILED, this);
		}
		private function parseList(src:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			for each(var rec:* in src) {
				var vo:ApprovalListVO = new ApprovalListVO;
				vo.BUKRS 			= rec.BUKRS;
				vo.GWAREKEY 		= rec.GWAREKEY;
				vo.PROCGBN			= rec.PROCGBN;
				vo.PROCGBN_TX		= rec.PROCGBN_TX;
				vo.TITLE			= rec.TITLE;
				vo.GWARELEV			= rec.GWARELEV;
				vo.GWARELEV_TX		= rec.GWARELEV_TX;
				vo.WSTATUS			= rec.WSTATUS;
				vo.WSTATUS_TX		= rec.WSTATUS_TX;
				vo.GWARE_OUTDATE	= DateUtil.stringToDateTime(rec.GWARE_OUTDATE);	// 결재 요청일
				vo.GWARE_OUTZEIT	= DateUtil.stringToDateTime(rec.GWARE_OUTZEIT);	// 요청시간
				vo.GWARE_OUTNAME	= rec.GWARE_OUTNAME;	// 결재 요청자 사번
				vo.GWARE_INDATE		= DateUtil.stringToDateTime(rec.GWARE_INDATE);		// 승인일
				vo.GWARE_INZEIT		= DateUtil.stringToDateTime(rec.GWARE_INZEIT);		// 승인시간
				vo.GWARE_INNAME		= rec.GWARE_INNAME;		// 예정승인자 사번
				vo.ENAME			= StringUtil.trim(rec.ENAME);			// 요청자 이름
				vo.WNAME			= rec.WNAME;			// 승인자 이름
				vo.EVID				= rec.EVID;
				list.addItem(vo);
			}
			return list;
		}
	}
}